package com.bos.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/12/11
 * Time: 2:28 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "pengelola")
@Cacheable(value = false)
public class Pengelola implements DomainObject, UserDetails {
    @Id
    @GeneratedValue
    private long id;

    @NotNull
    @Size(min = 1, max = 50)
    private String nama;

    @NotNull
    @Size(min=4, max=20)
    private String telp;

    @ManyToOne
    private Instansi instansi;

    @ManyToOne
    private Jabatan jabatan;

    @Size(min = 5, max = 20)
    @Column(name = "user_name", nullable = false, length = 20,unique = true)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "salt", nullable = true)
    private String salt;

    @ManyToOne
    @JoinColumn(name = "role", nullable = false)
    private Role role;

    @Column(name = "enabled", nullable = false)
    private boolean enabled;

    @Transient
    private boolean accountNonExpired = true;

    @Transient
    private boolean accountNonLocked = true;

    @Transient
    private boolean credentialsNonExpired = true;

    public Pengelola() {
    }

    public Pengelola(String nama, String telp, Instansi instansi, Jabatan jabatan) {
        this.nama = nama;
        this.telp = telp;
        this.instansi = instansi;
        this.jabatan = jabatan;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public String getTelp() {
        return telp;
    }

    public void setTelp(String telp) {
        this.telp = telp;
    }

    public Instansi getInstansi() {
        return instansi;
    }

    public void setInstansi(Instansi instansi) {
        this.instansi = instansi;
    }

    public Jabatan getJabatan() {
        return jabatan;
    }

    public void setJabatan(Jabatan jabatan) {
        this.jabatan = jabatan;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(role);
        return authorities;
    }

    public String getPassword() {
        return password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Pengelola)) return false;

        Pengelola pengelola = (Pengelola) o;

        if (accountNonExpired != pengelola.accountNonExpired) return false;
        if (accountNonLocked != pengelola.accountNonLocked) return false;
        if (credentialsNonExpired != pengelola.credentialsNonExpired) return false;
        if (enabled != pengelola.enabled) return false;
        if (id != pengelola.id) return false;
        if (instansi != null ? !instansi.equals(pengelola.instansi) : pengelola.instansi != null) return false;
        if (jabatan != null ? !jabatan.equals(pengelola.jabatan) : pengelola.jabatan != null) return false;
        if (nama != null ? !nama.equals(pengelola.nama) : pengelola.nama != null) return false;
        if (password != null ? !password.equals(pengelola.password) : pengelola.password != null) return false;
        if (role != null ? !role.equals(pengelola.role) : pengelola.role != null) return false;
        if (salt != null ? !salt.equals(pengelola.salt) : pengelola.salt != null) return false;
        if (telp != null ? !telp.equals(pengelola.telp) : pengelola.telp != null) return false;
        if (username != null ? !username.equals(pengelola.username) : pengelola.username != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + (nama != null ? nama.hashCode() : 0);
        result = 31 * result + (telp != null ? telp.hashCode() : 0);
        result = 31 * result + (instansi != null ? instansi.hashCode() : 0);
        result = 31 * result + (jabatan != null ? jabatan.hashCode() : 0);
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (salt != null ? salt.hashCode() : 0);
        result = 31 * result + (role != null ? role.hashCode() : 0);
        result = 31 * result + (enabled ? 1 : 0);
        result = 31 * result + (accountNonExpired ? 1 : 0);
        result = 31 * result + (accountNonLocked ? 1 : 0);
        result = 31 * result + (credentialsNonExpired ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Pengelola{" +
                "id=" + id +
                ", nama='" + nama + '\'' +
                ", telp='" + telp + '\'' +
                ", instansi=" + instansi +
                ", jabatan=" + jabatan +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", salt='" + salt + '\'' +
                ", role=" + role +
                ", enabled=" + enabled +
                ", accountNonExpired=" + accountNonExpired +
                ", accountNonLocked=" + accountNonLocked +
                ", credentialsNonExpired=" + credentialsNonExpired +
                '}';
    }
}
